//
//  ViewController.m
//  A_GOGO
//
//  Created by Liuiu on 16/1/15.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "ViewController.h"
#import <AFNetworking.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "MoreViewController.h"
#import "NetworkHandler.h"
#import "HomeModel.h"
#import "CarouselPicture.h"
#import "SearchViewController.h"
#import "RecommendCollectionViewCell.h"
#import "UIColor+Wonderful.h"
#import "NewCollectionViewCell.h"
#import "HotViewController.h"
#import "RecommendViewController.h"
#import "ConsultViewController.h"
//按钮点击实现"抽屉效果"导入的头文件
#import <UIViewController+MMDrawerController.h>
#import <MMDrawerBarButtonItem.h>
#import "LeftDrawerViewController.h"
@interface ViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic, retain)UILabel *label;
@property (nonatomic, retain)UIScrollView *scrollView;//首页
@property (nonatomic, retain)NSMutableArray *dataArr;//轮播图
@property (nonatomic, retain)NSMutableArray *arr;//储存今日推荐数据
@property (nonatomic, retain)NSMutableArray *newarr;//最新更新
@property (nonatomic, retain)NSMutableArray *marr;//今日推荐游戏照片
@property (nonatomic, retain)NSDictionary *dic;//存放8个button点击模块
@property (nonatomic, retain)UIPageControl *page;//轮播
@property (nonatomic, retain)NSTimer *time;//时间轴
@property (nonatomic, retain)UICollectionView *collection;//今日推荐
@property (nonatomic, retain)UICollectionView *newcollection;//最新更新
@property (nonatomic, retain)UIView *viewd;//背景

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //导航栏设置
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = NO;
    //设置导航栏字体和大小
    self.navigationItem.title = @"GOGO";
    [self.navigationController.navigationBar setTitleTextAttributes:
       @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:21],
    NSForegroundColorAttributeName:[UIColor grayColor]}];
    
    self.navigationController.navigationBar.translucent =NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];

    //右边"寻找"按键
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sousuo"] style:UIBarButtonItemStyleDone target:self action:@selector(right:)];
    self.navigationItem.rightBarButtonItems = @[item];
//****  想要单独设置右边的按钮颜色,在@[item]下边
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
    
    
    [item release];
    
    //左边"个人资料"点击
    UIButton *buttona = [UIButton buttonWithType:UIButtonTypeSystem];
    buttona.backgroundColor = [UIColor whiteColor];
    buttona.frame =  CGRectMake(0, 0, 38, 38);
    buttona.layer.cornerRadius = 19;
    [buttona setBackgroundImage:[UIImage imageNamed:@"personal"] forState:UIControlStateNormal];
    [buttona addTarget:self action:@selector(left:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttona];
    
    
//    scrollView
    
    self.scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scrollView.delegate = self;
    _scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(0, 1220);
    [self.view addSubview:_scrollView];
    [_scrollView release];
 
    
#pragma mark - 今日推荐collection

    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
    
    //滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
//    横向间距
    layout.minimumLineSpacing = 20;
    
    //每个cell大小
    layout.itemSize = CGSizeMake((375 - 20 - 20 ) / 2, 115);
    
    
    //边界
    layout.sectionInset = UIEdgeInsetsMake(2, 10, 13, 10);

    //创建
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 212, self.view.frame.size.width , 260) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    
   
    _collection.delegate = self;
    _collection.dataSource = self;
    
    _collection.pagingEnabled = YES;
    
    [layout release];
    [self.scrollView addSubview:_collection];
    [_collection release];
    
    //注册cell的重用池
    [_collection registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

//    pageController
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(20, 180, 40, 30)];
    _page.numberOfPages = 3;
    _page.currentPageIndicatorTintColor = [UIColor doderBlue];
    _page.pageIndicatorTintColor = [UIColor lightGrayColor];
    
    [self.scrollView addSubview:_page];
   
    [_page addTarget:self action:@selector(page:) forControlEvents:UIControlEventTouchUpInside];
    [_page release];
    
    
#pragma mark - New collection
    
    UICollectionViewFlowLayout *newlayout =[[UICollectionViewFlowLayout alloc]init];
    
    //滚动方向
    newlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    //横向之间的间距
    newlayout.minimumLineSpacing = 20;
    
    //每个cell的大小
    newlayout.itemSize = CGSizeMake((285 / 4.5), (285 / 4.5)+15);
    
    
    //边界
    newlayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    //创建
    self.newcollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 847, self.view.frame.size.width, 80) collectionViewLayout:newlayout];
    _newcollection.backgroundColor =[UIColor whiteColor];
    _newcollection.delegate =self;
    _newcollection.dataSource = self;

//  让collection的横向滑动消失
    _newcollection.showsHorizontalScrollIndicator = NO;
    
    [newlayout release];
    [self.scrollView addSubview:_newcollection];
    [_newcollection release];
    
    //注册新cell的重用池
    [_newcollection registerClass:[NewCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //处理数据
    [self data];
    
    
    
}
//页码控制器
- (void)page:(UIPageControl *)page
{
    self.collection.contentOffset = CGPointMake(page.currentPage *375, 0);
    
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    _page.currentPage = _collection.contentOffset.x / 360;
    
}

- (void)data
{

    
    NSString *str = @"http://open.youxiduo.com/home/?debug=beta&source=bc9d9f7784910a6766fadf6a267bf753&appname=yxdjqb&uid=6052381&os=ios&version=3.4.0";

    
#pragma mark - 最新更新collection
    [NetworkHandler GETWithURL:str completion:^(id result) {
        self.newarr =result[@"result"][@"newgames"];
        self.newarr = [HomeModel modelHanderWithArray:_newarr];
        [_newcollection reloadData];
        
   //只是为了美观
    UIView *viewb =[[UIView alloc]initWithFrame:CGRectMake(0, 785, self.viewd.frame.size.width, 15)];
    viewb.backgroundColor = [UIColor gainsboroColor];
    [self.scrollView addSubview:viewb];
    [viewb release];
        
    //标题
    UILabel *labelb =[[UILabel alloc]initWithFrame:CGRectMake(150, 780, 80, 30) ];
    labelb.center = CGPointMake(375 / 2, 815);
    labelb.text = @"最新更新";
    labelb.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.5];
    [self.scrollView addSubview:labelb];
    [labelb release];
        
    //按钮button
    UIButton *newbutton = [UIButton buttonWithType:UIButtonTypeSystem];
        newbutton.frame =CGRectMake(310, 808, 60, 12);
        [newbutton addTarget:self action:@selector(newbutton:) forControlEvents:UIControlEventTouchUpInside];
        [newbutton setTitle:@"更多 >" forState:UIControlStateNormal];
        [newbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.scrollView addSubview:newbutton];
        
        
     //只是为了美观
    UIView *viewc =[[UIView alloc]initWithFrame:CGRectMake(0, 945, self.viewd.frame.size.width, 15)];
    viewc.backgroundColor =[UIColor gainsboroColor];
    [self.scrollView addSubview:viewc];
    [viewc release];
    
        
    
    
    }];
#pragma mark - 8 BUTTON
       [NetworkHandler GETWithURL:str completion:^(id result) {
           self.dic = result[@"result"];
           
//********  灰色背景View
    _viewd = [[UIView alloc]initWithFrame:CGRectMake(0, 475, self.view.frame.size.width, 130)];
    _viewd.backgroundColor = [UIColor gainsboroColor];
    [self.scrollView addSubview:_viewd];
    [_viewd release];
           

           
//********    新游首发
           UIImageView *viewa = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 170, 90)];
           viewa.layer.cornerRadius = 10;
           
           HomeModel *m = [[HomeModel alloc] init];
           [m setValuesForKeysWithDictionary:_dic];
           [viewa sd_setImageWithURL:[NSURL URLWithString: m.xyqz_coverurl]];
           [self.viewd addSubview:viewa];
           
           [viewa release];
           
           UIButton *buttona = [UIButton buttonWithType:UIButtonTypeCustom];
           buttona.backgroundColor = [UIColor clearColor];
           [buttona addTarget:self action:@selector(newgame:) forControlEvents:UIControlEventTouchUpInside];
           buttona.frame = CGRectMake(10, 20, 170, 90);
           buttona.layer.cornerRadius = 10;
           [self.viewd addSubview:buttona];

//**********   资讯中心
           UIImageView *viewb =[[UIImageView alloc]initWithFrame:CGRectMake(195, 20, 170, 90)];
           viewb.layer.cornerRadius = 10;
           [viewb sd_setImageWithURL:[NSURL URLWithString:m.zxzx_coverurl]];
           [self.viewd addSubview:viewb];
           [viewb release];
           
           UIButton *buttonb = [UIButton buttonWithType:UIButtonTypeCustom];
           buttonb.backgroundColor =[UIColor clearColor];
           [buttonb addTarget:self action:@selector(zixun:) forControlEvents:UIControlEventTouchUpInside];
           buttonb.frame = CGRectMake(195, 20, 170, 90);
           buttonb.layer.cornerRadius = 10;
           [self.viewd addSubview: buttonb];
           
//***********  热门视频
           UIImageView *viewc =[[UIImageView alloc]initWithFrame:CGRectMake(10, 625, 110, 140)];
           viewc.layer.cornerRadius = 10;
           [viewc sd_setImageWithURL:[NSURL URLWithString:m.mnsp_coverurl]];
           [self.scrollView addSubview:viewc];
           [viewc release];
           
           UIButton *buttonc =[UIButton buttonWithType:UIButtonTypeCustom];
           buttonc.backgroundColor =[UIColor clearColor];
           [buttonc addTarget:self action:@selector(hot:) forControlEvents:UIControlEventTouchUpInside];
           buttonc.frame =CGRectMake(10, 625, 110, 140);
           buttonc.layer.cornerRadius = 10;
           [self.scrollView addSubview:buttonc];
           
//***********  特色专辑
           UIImageView *viewd = [[UIImageView alloc]initWithFrame:CGRectMake(132.5, 625, 110, 140)];
           viewd.layer.cornerRadius = 10;
           [viewd sd_setImageWithURL:[NSURL URLWithString:m.tszt_coverurl]];
           [self.scrollView addSubview:viewd];
           [viewd release];
           
           UIButton *buttond =[UIButton buttonWithType:UIButtonTypeCustom];
           buttond.backgroundColor =[UIColor clearColor];
           [buttond addTarget:self action:@selector(special:) forControlEvents:UIControlEventTouchUpInside];
           buttond.frame =CGRectMake(132.5, 625, 110, 140);
           buttond.layer.cornerRadius = 10;
           [self.scrollView addSubview:buttond];
           
//***********  经典必玩
           UIImageView *viewe =[[UIImageView alloc]initWithFrame:CGRectMake(255, 625, 110, 140)];
           viewe.layer.cornerRadius = 10;
           [viewe sd_setImageWithURL:[NSURL URLWithString:m.jdbw_coverurl]];
           [self.scrollView addSubview:viewe];
           [viewe release];
           
           UIButton *buttone =[UIButton buttonWithType:UIButtonTypeCustom];
           buttone.backgroundColor =[UIColor clearColor];
           [buttone addTarget:self action:@selector(best:) forControlEvents:UIControlEventTouchUpInside];
           buttone.frame =CGRectMake(255, 625, 110, 140);
           buttone.layer.cornerRadius = 10;
           [self.scrollView addSubview:buttone];
           
//***********  社区广场
           UILabel *labela =[[UILabel alloc]initWithFrame:CGRectMake(150, 960, 80, 30)];
           labela.center = CGPointMake(self.view.frame.size.width / 2, 975);
           labela.text = @"社区广场";
           labela.font =[UIFont fontWithName:@"Helvetica-Bold" size:18.5];
           [self.scrollView addSubview:labela];
           [labela release];
           
           
           // 八卦吐槽
           UIImageView *viewf =[[UIImageView alloc]initWithFrame:CGRectMake(10,995, 110, 140)];
           viewf.layer.cornerRadius = 10;
           [viewf sd_setImageWithURL:[NSURL URLWithString:m.yxx_coverurl]];
           [self.scrollView addSubview:viewf];
           [viewf release];
           
           UIButton *buttonf =[UIButton buttonWithType:UIButtonTypeCustom];
           buttonf.backgroundColor =[UIColor clearColor];
           [buttonf addTarget:self action:@selector(luan:) forControlEvents:UIControlEventTouchUpInside];
           buttonf.frame =CGRectMake(10, 1015, 110, 140);
           buttonf.layer.cornerRadius = 10;
           [self.scrollView addSubview:buttonf];

           // 寻找伙伴
           UIImageView *viewg =[[UIImageView alloc]initWithFrame:CGRectMake(132.5, 995, 110, 140)];
           viewg.layer.cornerRadius = 10;
           [viewg sd_setImageWithURL:[NSURL URLWithString:m.xrbd_coverurl]];
           [self.scrollView addSubview:viewg];
           [viewg release];
           
           UIButton *buttong =[UIButton buttonWithType:UIButtonTypeCustom];
           buttong.backgroundColor =[UIColor clearColor];
           [buttong addTarget:self action:@selector(patriers:) forControlEvents:UIControlEventTouchUpInside];
           buttong.frame =CGRectMake(132.5, 1015, 110, 140);
           buttong.layer.cornerRadius = 10;
           [self.scrollView addSubview:buttong];

           // 游戏问题
           UIImageView *viewh =[[UIImageView alloc]initWithFrame:CGRectMake(255, 995, 110, 140)];
           viewh.layer.cornerRadius = 10;
           [viewh sd_setImageWithURL:[NSURL URLWithString:m.yxwd_coverurl]];
           [self.scrollView addSubview:viewh];
           [viewh release];
           
           UIButton *buttonh =[UIButton buttonWithType:UIButtonTypeCustom];
           buttonh.backgroundColor =[UIColor clearColor];
           [buttonh addTarget:self action:@selector(question:) forControlEvents:UIControlEventTouchUpInside];
           buttonh.frame =CGRectMake(255, 1015, 110, 140);
           buttonh.layer.cornerRadius = 10;
           [self.scrollView addSubview:buttonh];

       }];

    
    
    
#pragma mark - 今日推荐
    [NetworkHandler GETWithURL:str completion:^(id result) {
        self.arr =result[@"result"][@"hotgames"];
        self.arr = [HomeModel modelHanderWithArray:_arr];
        [_collection reloadData];
    }];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(120, 180, 80, 30)];
    label.center = CGPointMake(self.view.frame.size.width / 2, 198);
    label.text = @"热门推荐";
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.5];
    [self.scrollView addSubview:label];
    [label release];
    
//    "更多 >"按钮
    UIButton *buttont = [UIButton buttonWithType:UIButtonTypeSystem];
    buttont.frame = CGRectMake(310, 183, 60, 30);
//    button.backgroundColor = [UIColor redColor];
    [buttont setTitle:@"更多 >" forState:UIControlStateNormal];
    [buttont setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [buttont addTarget:self action:@selector(more:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:buttont];
    
    
    
    
    
#pragma mark - 首页轮播图
    
    [NetworkHandler GETWithURL:str completion:^(id result) {
        self.dataArr = result[@"result"][@"shuffles"];
        self.dataArr = [HomeModel modelHanderWithArray:_dataArr];
     
      
        
//        调用封装轮播图方法
        
        NSMutableArray *photoArr =[NSMutableArray array];
        for (HomeModel *m in _dataArr) {
            [photoArr addObject:m.img];
        }
        
        CarouselPicture *picture = [[CarouselPicture alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180) timeInterval:2.6 imageUrlArr:photoArr urlArr:nil];
//          [self.scrollView reloadData];
        [self.scrollView addSubview:picture];
        [picture release];
 
    }];
    
}



//collectionview的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _collection) {
        return _arr.count;
    }else{
        return _newarr.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _collection) {
        
      
        
        RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        
        //    给cell添加边框~
        cell.layer.cornerRadius = self.view.frame.size.width / 25;
        cell.layer.borderColor = [UIColor silverColor].CGColor;
        cell.layer.borderWidth = 0.2;
        HomeModel *y = _arr[indexPath.row];
        
        cell.homeM = y;
        return cell;
       
    }else{
        NewCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        HomeModel *l = _newarr[indexPath.row];
        [cell.Timg sd_setImageWithURL:[NSURL URLWithString:l.img]];
        cell.title.text = l.title;
        
        
        return cell;
    }
    
    //什么都不返回
    return NULL;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collection) {
        //让tabBar消失
        self.hidesBottomBarWhenPushed=YES;
       RecommendViewController *sec =[[RecommendViewController alloc]init];
        
        [self.navigationController pushViewController:sec animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
}


#pragma mark - 按钮区

//最新更新
- (void)newbutton:(UIButton *)btn
{
    NSLog(@"我来了");
}

//游戏问题
- (void)question:(UIButton *)btn
{
    NSLog(@"游戏问题");
}


//寻找伙伴
- (void)patriers:(UIButton *)btn
{
    NSLog(@"寻找伙伴");
}

//八卦吐槽
- (void)luan:(UIButton *)btn
{
    NSLog(@"八卦吐槽");
}

//经典必玩
- (void)best:(UIButton *)btn
{
    NSLog(@"经典必玩");
}


//特色专辑
- (void)special:(UIButton *)btn
{
    NSLog(@"特色专辑");
}


//热门视频
- (void)hot:(UIButton *)btn
{
    HotViewController *sec =[[HotViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
}



//资讯中心
- (void)zixun:(UIButton *)btn
{
    NSLog(@"资讯中心");
    ConsultViewController *sec =[[ConsultViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
    
}

//新游首发
- (void)newgame:(UIButton *)btn
{
    NSLog(@"新游首发");
}

- (void)more:(UIButton *)btn
{
    MoreViewController *more = [[MoreViewController alloc]init];
    [self.navigationController pushViewController:more animated:YES];
    
}

//导航栏右边搜索按钮
- (void)right:(UIBarButtonItem *)btn
{
    SearchViewController *sec =[[SearchViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
    
    
}
//导航栏左边个人资料按钮
- (void)left:(UIButton *)btn
{
    
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
        
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
